﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace LastfmNet.Models
{
    public class LastfmPager<T> : LastfmModelBase, ILastfmPageable where T : LastfmModelBase, new()
    {
        private string itemsXmlName;

        public LastfmPager()
        {
            this.Page = 0;
            this.PerPage = 0;
            this.TotalPages = 0;
            this.Total = 0;
            this.itemsXmlName = null;
            this.ItemsList = new List<T>();
        }

        public LastfmPager(XmlNode node)
            : this()
        {
            if (node == null) return;
            this.InitializeFromXmlNode(node);
        }

        public LastfmPager(XmlNode node, string itemsXmlName)
            : this()
        {
            if (node == null) return;
            this.itemsXmlName = itemsXmlName;
            this.InitializeFromXmlNode(node);
        }

        public override void InitializeFromXmlNode(XmlNode node)
        {
            if (node == null) return;
            this.Page = node.GetAttributeValue("page").ToInteger();
            this.PerPage = node.GetAttributeValue("perPage").ToInteger();
            this.TotalPages = node.GetAttributeValue("totalPages").ToInteger();
            this.Total = node.GetAttributeValue("total").ToInteger();
            this.ItemsList = LastfmModelBase.GetList<T>(node, itemsXmlName);
        }

        public int Page { get; set; }

        public int PerPage { get; set; }

        public int TotalPages { get; set; }

        public int Total { get; set; }

        public IList<T> ItemsList { get; set; }
    }
}
